package pointshop;


import org.bukkit.event.Listener;

import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.ServicesManager;
import org.bukkit.plugin.java.JavaPlugin;


import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

public class Main extends JavaPlugin  implements Listener {
    public static Main plugin;
    private ServicesManager sm;
    static Store store;

    private static final Logger log = Logger.getLogger("Minecraft");
    public void onEnable()
    {
        plugin = this;
        this.sm = getServer().getServicesManager();
        loadPoint();
        File file = new File(getDataFolder(), "Shop.yml");
    if (!file.exists()) {
     saveResource("Shop.yml", false);
    }

        getLogger().info("PointShop_v1.0已加载");
        if (!getDataFolder().exists())
        {
            getDataFolder().mkdirs();
        }
        getServer().getPluginManager().registerEvents(this, this);
        getServer().getPluginManager().registerEvents(Main.store = new Store(this), this);
        getCommand("djshop").setExecutor(new CommandListener(this));
    }
   private void loadPoint() {
        hookPoint("PointSystem", Point_system.class, ServicePriority.High, new String[]{"mcps.bukkit.pointsystem"});

    }
    private void hookPoint(String name, Class<? extends Points> hookClass, ServicePriority priority, String[] packages) {
        try {
            if (packagesExists(packages)) {
                Points econ = (Points) hookClass.getConstructor(new Class[]{Plugin.class}).newInstance(new Object[]{this});
                this.sm.register(Points.class, econ, this, priority);


            }
        } catch (Exception e) {

        }
    }

    private static boolean packagesExists(String[] packages) //检测包是否存在
    {
        try {
            for (String pkg : packages) {
                Class.forName(pkg);
            }
            return true;
        } catch (Exception e) {
        }
        return false;
    }
  }
